package com.my.study.design.rule;

import java.util.List;

/**
 * 规则执行器
 *
 * @author: yidujun
 * @create: 2021/07/10 00:51
 */
public class RulesExecutor {
    private List<IRule> rules;

    public RulesExecutor(List<IRule> rules) {
        this.rules = rules;
    }

    public Object execute(Object obj) {
        Object newObj = obj;
        label:
        for (IRule rule : rules) {
            RuleExecuteResult ruleExecuteResult  = rule.execute(obj);
            newObj = ruleExecuteResult.getResult();
            switch (ruleExecuteResult.getEnumRuleResult()) {
                case CONTINUE:
                    break;
                case RETURN:
                    break label;
                case ABANDON:
                    return newObj;
                default:
            }
        }
        return newObj;
    }

    public List<IRule> getRules() {
        return rules;
    }

    public void setRules(List<IRule> rules) {
        this.rules = rules;
    }

    public static void main(String[] args) {
        System.out.println("====label之前====");
        label:
        for (int i = 0; i < 10; i++) {
            switch (i) {
                case 0:
                    System.out.println("0 ~~~~~~~");
                    break;
                case 1:
                    System.out.println("1 ~~~~~~~");
                    break;
                case 5:
                    continue label;
                default:
                    System.out.println(i);
                    break ;
            }
        }
        System.out.println("===结束==");


    }

}
